<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="productTypeId">
        <el-select v-model="queryParams.productTypeId" placeholder="请输入出行方式" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="type in typeOptions" :key="type.typeId" :label="type.typeName" :value="type.typeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="持续天数" prop="durationDays">
        <el-input
          v-model="queryParams.durationDays"
          placeholder="请输入持续天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="bookingTime">
        <el-date-picker clearable
          v-model="queryParams.bookingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出发时间" prop="departureDate">
        <el-date-picker clearable
          v-model="queryParams.departureDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出发时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最少人数" prop="minTravelers">
        <el-input
          v-model="queryParams.minTravelers"
          placeholder="请输入最少人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出行方式" prop="travelMode">
        <el-select
          v-model="queryParams.travelMode"
          placeholder="请输入出行方式" 
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option v-for="dict in dict.type.tourism_trip_mode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="票价" prop="adultPrice">
        <el-input
          v-model="queryParams.adultPrice"
          placeholder="请输入票价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tourism:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tourism:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tourism:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tourism:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编号" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品类型" align="center" prop="productTypeId">
        <template slot-scope="scope">
          <span>{{ handleType(scope.row.productTypeId,typeOptions) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="目的地" align="center" prop="destination" />
      <el-table-column label="持续天数" align="center" prop="durationDays" />
      <el-table-column label="预约时间" align="center" prop="bookingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发时间" align="center" prop="departureDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最少人数" align="center" prop="minTravelers" />
      <el-table-column label="出行方式" align="center" prop="travelMode" >
        <template slot-scope="scope">
          <span>{{ handleDicts(scope.row.travelMode,dict.type.tourism_trip_mode) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="票价" align="center" prop="adultPrice" />
      <el-table-column label="产品状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tourism:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tourism:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品描述" prop="productDescription">
              <el-input v-model="form.productDescription" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品类型" prop="productTypeId">
              <el-select v-model="form.productTypeId" placeholder="请输入出行方式">
                <el-option v-for="type in typeOptions" :key="type.typeId" :label="type.typeName" :value="type.typeId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目的地" prop="destination">
              <el-input v-model="form.destination" placeholder="请输入目的地" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出发城市" prop="departureCity">
              <el-input v-model="form.departureCity" placeholder="请输入出发城市" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="持续天数" prop="durationDays">
              <el-input v-model="form.durationDays" placeholder="请输入持续天数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约时间" prop="bookingTime">
              <el-date-picker clearable
                v-model="form.bookingTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择预约时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出发时间" prop="departureDate">
              <el-date-picker clearable
                v-model="form.departureDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择出发时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最少人数" prop="minTravelers">
              <el-input v-model="form.minTravelers" placeholder="请输入最少人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最多人数" prop="maxTravelers">
              <el-input v-model="form.maxTravelers" placeholder="请输入最多旅行人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出行方式" prop="travelMode">
              <el-select v-model="form.travelMode" placeholder="请输入出行方式" >
                <el-option v-for="dict in dict.type.tourism_trip_mode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="酒店情况" prop="hotel">
              <el-input v-model="form.hotel" placeholder="请输入酒店情况" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票价" prop="adultPrice">
              <el-input v-model="form.adultPrice" placeholder="请输入票价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="儿童票价" prop="childPrice">
              <el-input v-model="form.childPrice" placeholder="请输入儿童票价" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/tourism/product";
import { listType } from "@/api/tourism/type";

export default {
  name: "Product",
  dicts:["tourism_trip_mode"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      //类型下拉框数据
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productTypeId: null,
        destination: null,
        durationDays: null,
        bookingTime: null,
        departureDate: null,
        minTravelers: null,
        travelMode: null,
        hotel: null,
        adultPrice: null,
        childPrice: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        productTypeId: [
          { required: true, message: "产品类型不能为空", trigger: "blur" }
        ],
        departureCity: [
          { required: true, message: "出发城市不能为空", trigger: "change" }
        ],
        destination: [
          { required: true, message: "目的地不能为空", trigger: "change" }
        ],
        durationDays: [
          { required: true, message: "持续天数不能为空", trigger: "blur" }
        ],
        departureDate: [
          { required: true, message: "出发时间不能为空", trigger: "blur" }
        ],
        minTravelers: [
          { required: true, message: "最少人数不能为空", trigger: "blur" }
        ],
        adultPrice: [
          { required: true, message: "票价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTypeOption();
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTypeOption(){
      this.loading = true;
      listType().then(response => {
        this.typeOptions = response.data;
        this.loading = false;
        
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productDescription: null,
        productTypeId: null,
        departureCity: null,
        destination: null,
        durationDays: null,
        bookingTime: null,
        departureDate: null,
        minTravelers: null,
        maxTravelers: null,
        travelMode: null,
        hotel: null,
        adultPrice: null,
        childPrice: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认删除产品管理编号为"' + productIds + '"的数据项？').then(function() {
        return delProduct(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
    handleDicts(value,dicts){
      let label = '';
      dicts.forEach(item => {
        if(item.value==value){
          label= item.label;
        }
      });
      return label;
    },
    handleType(value,types){
      let type = '';
      types.forEach(item => {
        if(item.typeId==value){
          type= item.typeName;
        }
      });
      return type;
    }
  }
};
</script>
